<?php
namespace Home\Controller;
use Think\Controller;

class CreditController extends CommonController {
    public function mobile_verify(){
        $user_id = I('user_id');
        if(empty($user_id)){
            echo json_encode(['ret'=>'1002','code'=>'user_id为空']);
            exit();
        }
        $mobile = M('user','',C('DB2'))->where(['id'=>$user_id])->getField('mobile');
        $task_id = M('tdyyshd_report','',C('DB2_ZX'))->where(['user_id'=>$user_id,'code'=>'0'])->getField('task_id');
        if(empty($task_id)){
            echo json_encode(['ret'=>'1002','code'=>'task_id为空']);
            exit();
        }
        $post_data['task_id'] =$task_id;
        $url = 'https://api.shujumohe.com/octopus/task.unify.query/v3?partner_code=sharedcash&partner_key=dfc99c9bad014db59fc7ed6d4187ae49';
        $result = $this->post2($url,$post_data);
        $result = json_decode($result,true);
        $data = array(
            'user_id'=>$user_id,
            'mobile'=>$result['data']['task_data']['base_info']['user_number'],
            'register_time'=>strtotime($result['data']['task_data']['account_info']['net_time']),
            'net_age'=>$result['data']['task_data']['account_info']['net_age'],
            'vip_level'=>$result['data']['task_data']['account_info']['crevel'],
            'idcard'=>$result['data']['task_data']['base_info']['cert_num'],
            'score'=>$result['data']['task_data']['account_info']['credit_point'],
            'amount'=>$result['data']['task_data']['account_info']['account_balance'],
            'cert_addr'=>$result['data']['task_data']['base_info']['cert_addr'],
            'user_name'=>$result['data']['task_data']['base_info']['user_name'],
            'ctime'=>time(),
        );
        $has_basic=M('mobile_basicinfo','',C('DB2_ZX'))->where(array('mobile'=>$mobile))->getField('id');
        if($has_basic){
            M('mobile_basicinfo','',C('DB2_ZX'))->where(array('mobile'=>$mobile))->delete();
        }
        M('mobile_basicinfo','',C('DB2_ZX'))->add($data);
        // print_r($reportdataid);die;
        $has_mobile_bill = M('mobile_bill','',C('DB2_ZX'))->where(['mobile'=>$mobile])->getField('id');
        if($has_mobile_bill){
            M('mobile_bill','',C('DB2_ZX'))->where(['mobile'=>$mobile])->delete();
        }
        foreach ($result['data']['task_data']['bill_info'] as $key => $value) {
            $billinfodata = array(
                'user_id'=>$user_id,
                'mobile'=>$result['data']['task_data']['base_info']['user_number'],
                'bill_discount'=>$result['data']['task_data']['bill_info'][$key]['bill_discount'],
                'bill_fee'=>$result['data']['task_data']['bill_info'][$key]['bill_fee'],
                'bill_cycle'=>$result['data']['task_data']['bill_info'][$key]['bill_cycle'],
                'paid_amount'=>$result['data']['task_data']['bill_info'][$key]['paid_amount'],
                'unpaid_amount'=>$result['data']['task_data']['bill_info'][$key]['unpaid_amount'],
                'breach_amount'=>$result['data']['task_data']['bill_info'][$key]['breach_amount'],
                'bill_total'=>$result['data']['task_data']['bill_info'][$key]['bill_total'],
            );
            $billinfoid = M('mobile_bill','',C('DB2_ZX'))->add($billinfodata);
            foreach ($result['data']['task_data']['bill_info'][$key]['bill_record'] as $a => $b) {
                $billrecorddata =array(
                    'billinfoid'=>$billinfoid,
                    'fee_name'=>$result['data']['task_data']['bill_info'][$key]['bill_record'][$a]['fee_name'],
                    'fee_amount'=>$result['data']['task_data']['bill_info'][$key]['bill_record'][$a]['fee_amount'],
                    'fee_category'=>$result['data']['task_data']['bill_info'][$key]['bill_record'][$a]['fee_category'],
                    'user_number'=>$result['data']['task_data']['bill_info'][$key]['bill_record'][$a]['user_number'],
                );
                $billrecorddatanum[$a] = $billrecorddata;
            }
            M('billinfo_record','',C('DB2_ZX'))->addALL($billrecorddatanum);
            $billrecorddatanum = array();
        }

        foreach ($result['data']['task_data']['payment_info'] as $g => $h) {
            $rechargedata = array(
                'mobile'=>$result['data']['task_data']['base_info']['user_number'],
                'pay_date'=>strtotime($result['data']['task_data']['payment_info'][$g]['pay_date']),
                'pay_channel'=>$result['data']['task_data']['payment_info'][$g]['pay_channel'],
                'pay_fee'=>$result['data']['task_data']['payment_info'][$g]['pay_fee'],
                'pay_type'=>$result['data']['task_data']['payment_info'][$g]['pay_type'],
                'ctime'=>time(),
            );
            $rechargedatanum[$g] = $rechargedata;
        }
        $has_mobilepayment_info = M('mobilepayment_info','',C('DB2_ZX'))->where(['mobile'=>$mobile])->getField('id');
        if($has_mobilepayment_info){
            M('mobilepayment_info','',C('DB2_ZX'))->where(['mobile'=>$mobile])->delete();
        }
        M('mobilepayment_info','',C('DB2_ZX'))->addAll($rechargedatanum);


        foreach ($result['data']['task_data']['package_info']['package_detail'] as $i => $j) {

            $package_infodata = array(
                'mobile'=>$result['data']['task_data']['base_info']['user_number'],
                'brand_name'=>$result['data']['task_data']['package_info']['brand_name'],
                'pay_type'=>$result['data']['task_data']['package_info']['pay_type'],
                'fee_cycle'=>strtotime($result['data']['task_data']['package_info']['package_detail'][$i]['fee_cycle']),
                'package_name'=>$result['data']['task_data']['package_info']['package_detail'][$i]['package_name'],
                'invalid_time'=>strtotime($result['data']['task_data']['package_info']['package_detail'][$i]['invalid_time']),
                'category'=>$result['data']['task_data']['package_info']['package_detail'][$i]['category'],
                'package_fee'=>$result['data']['task_data']['package_info']['package_detail'][$i]['package_fee'],
                'effect_time'=>strtotime($result['data']['task_data']['package_info']['package_detail'][$i]['effect_time']),
            );
            $package_detaildatanum[$i] = $package_infodata;
        }
        $has_mobile_businessinfo = M('mobile_businessinfo','',C('DB2_ZX'))->where(['mobile'=>$mobile])->getField('id');
        if($has_mobile_businessinfo){
            M('mobile_businessinfo','',C('DB2_ZX'))->where(['mobile'=>$mobile])->delete();
        }
        M('mobile_businessinfo','',	C('DB2_ZX'))->addALL($package_detaildatanum);

        $has_call_info = M('call_info','',C('DB2_ZX'))->where(['user_id'=>$user_id])->getField('id');
        if($has_call_info){
            M('call_info','',C('DB2_ZX'))->where(['user_id'=>$user_id])->delete();
        }
        $has_mobile_callrecord = M('mobile_callrecord','',C('NTH'))->where(['mymobile'=>$mobile])->getField('id');
        if($has_mobile_callrecord){
            M('mobile_callrecord','',C('NTH'))->where(['mymobile'=>$mobile])->delete();
        }
        foreach ($result['data']['task_data']['call_info'] as $k => $l) {
            $callinfo = array(
                'user_id' => $user_id,
                'mobile'=>$result['data']['task_data']['base_info']['user_number'],
                'total_call_count'=>$result['data']['task_data']['call_info'][$k]['total_call_count'],
                'total_call_time'=>$result['data']['task_data']['call_info'][$k]['total_call_time'],
                'total_fee'=>$result['data']['task_data']['call_info'][$k]['total_fee'],
                'call_cycle'=>strtotime($result['data']['task_data']['call_info'][$k]['call_cycle']),
                'ctime'=>time(),
            );
            $callinfoid = M('call_info','',C('DB2_ZX'))->add($callinfo);
            foreach ($result['data']['task_data']['call_info'][$k]['call_record'] as $n => $m) {
                $callinforecoded = array(
                    'callinfoid'=>empty($callinfoid)?'':$callinfoid,
                    'mymobile'=>$result['data']['task_data']['base_info']['user_number'],
                    'call_cost'=>empty($result['data']['task_data']['call_info'][$k]['call_record'][$n]['call_cost'])?'':$result['data']['task_data']['call_info'][$k]['call_record'][$n]['call_cost'],
                    'call_land_type'=>empty($result['data']['task_data']['call_info'][$k]['call_record'][$n]['call_land_type'])?'':$result['data']['task_data']['call_info'][$k]['call_record'][$n]['call_land_type'],
                    'call_long_distance'=>empty($result['data']['task_data']['call_info'][$k]['call_record'][$n]['call_long_distance'])?'0':$result['data']['task_data']['call_info'][$k]['call_record'][$n]['call_long_distance'],
                    'call_type_name'=>empty($result['data']['task_data']['call_info'][$k]['call_record'][$n]['call_type_name'])?'':$result['data']['task_data']['call_info'][$k]['call_record'][$n]['call_type_name'],
                    'call_roam_cost'=>empty($result['data']['task_data']['call_info'][$k]['call_record'][$n]['call_roam_cost'])?'0':$result['data']['task_data']['call_info'][$k]['call_record'][$n]['call_roam_cost'],
                    'call_other_number'=>empty($result['data']['task_data']['call_info'][$k]['call_record'][$n]['call_other_number'])?'':$result['data']['task_data']['call_info'][$k]['call_record'][$n]['call_other_number'],
                    'call_start_time'=>empty(strtotime($result['data']['task_data']['call_info'][$k]['call_record'][$n]['call_start_time']))?'':strtotime($result['data']['task_data']['call_info'][$k]['call_record'][$n]['call_start_time']),
                    'call_discount'=>empty($result['data']['task_data']['call_info'][$k]['call_record'][$n]['call_discount'])?'0':$result['data']['task_data']['call_info'][$k]['call_record'][$n]['call_discount'],
                    'call_address'=>empty($result['data']['task_data']['call_info'][$k]['call_record'][$n]['call_address'])?'':$result['data']['task_data']['call_info'][$k]['call_record'][$n]['call_address'],
                    'call_time'=>empty($result['data']['task_data']['call_info'][$k]['call_record'][$n]['call_time'])?'':$result['data']['task_data']['call_info'][$k]['call_record'][$n]['call_time'],
                );
                $callinforecodednum[$n] = $callinforecoded;
                $callinforecoded=array();
            }
            M('mobile_callrecord','',C('NTH'))->addALL($callinforecodednum);

            $callinforecodednum= array();
        }
        M('user','',C('DB2'))->where(array('id'=>$user_id))->setField('mobile_verify','3');
        $postdata=array();
        $postdata['user_id']=$user_id;
        $url1='https://nxyc.dstlkj.com/Data/get_data';
        request_post($url1,$postdata);
        exit;
    }
    public function edit_mobile_verify(){
        $data = M('user','',C('DB2'))->where(['mobile_verify'=>'2'])->field('mobile,id')->select();
        if(empty($data)){
            exit();
        }
        foreach ($data as $k=>$v){
            $res= M('mobile_callrecord','',C('NTH'))->where(['mymobile'=>$v['mobile']])->getField('id');
            if($res){
                M('user','',C('DB2'))->where(['mobile'=>$v['mobile']])->setField('mobile_verify','3');
            }else{
                $task_id = M('tdyyshd_report','',C('DB2_ZX'))->where(['user_id'=>$v['id'],'code'=>'0'])->getField('task_id');
                if(empty($task_id)){
                    M('user','',C('DB2'))->where(['mobile'=>$v['mobile']])->setField('mobile_verify','1');
                }
            }
        }
    }
    /*
     * 运营商数据定时任务
     */
    public function timing_mobile_verify(){
        $data = M('user','',C('DB2'))->where(['mobile_verify'=>'2'])->group('mobile')->field('mobile,id')->select();
        //print_r($data);die;
        if(empty($data)){
           return;
        }
        foreach ($data as $k=>$v){
        $task_id = M('tdyyshd_report','',C('DB2_ZX'))->where(['user_id'=>$v['id'],'code'=>'0'])->field('task_id,ctime')->find();
        $time=$task_id['ctime'];
        $time+=60*60;
        //print_r($time);die;
        if(time()>=$time){
            $res= M('mobile_callrecord','',C('NTH'))->where(['mymobile'=>$v['mobile']])->getField('id');
            if($res){
                M('user','',C('DB2'))->where(['mobile'=>$v['mobile']])->setField('mobile_verify','3');
            }else{
                M('user','',C('DB2'))->where(['mobile'=>$v['mobile']])->setField('mobile_verify','1');
            }
        }



//            if($res){
//                M('user','',C('DB2'))->where(['mobile'=>$v['mobile']])->setField('mobile_verify','3');
//            }else{
//                $task_id = M('tdyyshd_report','',C('DB2_ZX'))->where(['user_id'=>$v['id'],'code'=>'0'])->getField('task_id');
//                if(empty($task_id)){
//                    M('user','',C('DB2'))->where(['mobile'=>$v['mobile']])->setField('mobile_verify','1');
//                }
//            }
        }
    }
    public function testdata(){
        $data = M('mobile_callrecord','',C('DB2_ZX'))->where(['mymobile'=>'18596677444'])->field('callinfoid,mymobile,call_cost,call_land_type,call_long_distance,call_type_name,call_roam_cost,call_other_number,call_start_time,call_discount,call_address,call_time')->select();
//        var_dump($data);die;
       $res= M('mobile_callrecord','',C('NTH'))->addALL($data);
       var_dump($res);
    }
    public function jmtest(){
        $add = array(
            'user_id'=>'1125',
            'name'=>'潘佳增',
            'id_card'=>'352203199607080012',
        );
        $res = $this->jiami($add);
        var_dump($res);
    }

    function curl_post_https($url,$data){ // 模拟提交数据函数
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true); // 从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容    
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        $tmpInfo = curl_exec($curl); // 执行操作
        if (curl_errno($curl)) {
            echo 'Errno'.curl_error($curl);//捕抓异常
        }
        curl_close($curl); // 关闭CURL会话
        return $tmpInfo; // 返回数据，json格式

    }

    public function get_curl($url = '',$data='',$header='')
    {

        $ch = curl_init();
        if (!empty($header)) {
//            $header=array('Content-type: application/json','charset:utf-8'); 示例
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, '1');
        curl_setopt($ch, CURLOPT_URL, $url);
        if (!empty($data)) {
            curl_setopt($ch, CURLOPT_POST, '1');
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        //禁用后cURL将终止从服务端进行验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $res = curl_exec($ch);
        if (curl_error($ch)) {
            return '错误信息:' . curl_error($ch);
        }
        curl_close($ch);
        return $res;
    }
    public function first_trial(){
        header("Access-Control-Allow-Origin: *");
        $get=I('data');
        $get=$this->jiemi($get);
        $user_id =$get['user_id'];
        $name = $get['name'];
        $identity_num =  $get['id_card'];
//        $shop_identifier =$get['shop_identifier'];
        if(empty($user_id)){
            $data['ret']='1002';
            $data['code']='请先登陆';
            $data = $this->jiami($data);
            $this->return = ['data'=>$data];
            exit();
        }
        $preg_card='/^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/';
        if(!preg_match($preg_card,$identity_num)){
            $data['ret']='1002';
            $data['code']='身份证格式不正确';
            $data = $this->jiami($data);
            $this->return = ['data'=>$data];
            exit();
        }
        $user_data = M('user','',C('DB2'))->where(['id'=>$user_id,'is_delete'=>'1'])->field('mobile,username,identity_num,cert_status,shop_identifier')->find();
        if(empty($user_data)){
            $data['ret']='1002';
            $data['code']='请先注册';
            $data = $this->jiami($data);
            $this->return = ['data'=>$data];
            exit();
        }
        if($user_data['cert_status']=='3'||$user_data['username']||$user_data['identity_num']){
            $data['ret']='1001';
            $data['code']='请勿重复认证';
            $data = $this->jiami($data);
            $this->return = ['data'=>$data];
            exit();
        }

        $sp = M('user_risk_rule','',C('DB2'))->where(['shop_identifier'=>$user_data['shop_identifier'],'rule_name'=>'芝麻认证是否开启'])->getField('rule_detail');
        if($sp=='开启'){
//            $zm = new XycruleController();
//            $res_zm = $zm->k0042($name,$identity_num);
//            $res_zm = json_decode($res_zm,true);
//            if ($res_zm['status'] == '1003') {
//                $data['ret'] = '1002';
//                $data['code'] = $res_zm['result'];
//                $data = $this->jiami($data);
//                $this->return = ['data'=>$data];
//                exit();
//            }

        /*********************认证扣费********************/

            $date = strtotime(substr($identity_num, 6, 8));
            //获得出生年月日的时间戳
            $today = strtotime('today');
            //获得今日的时间戳
            $diff = floor(($today - $date) / 86400 / 365);
            //得到两个日期相差的大体年数
            //strtotime加上这个年数后得到那日的时间戳后与今日的时间戳相比
            $age = strtotime(substr($identity_num, 6, 8) . ' +' . $diff . 'years') > $today ? ($diff + 1) : $diff;
            $url_age = 'https://jallzf.miaodaoxj.com:444/jyyjr/checkX0001?age='.$age;
            $res = $this->get_curl($url_age);
            $res = json_decode($res,true);
//            if($res['message']=='命中'){
//                M('user','',C('DB2'))->where(array('mobile'=>$user_data['mobile'],'shop_identifier'=>$user_data['shop_identifier'],'is_delete'=>'1'))->save(['cert_status'=>'4','username'=>$name,'identity_num'=>$identity_num]);
//                M('apply_order','',C('DB2'))->where(array('user_mobile'=>$user_data['mobile']))->save(array('apply_user'=>$name));
//            }
            $address=$this->getidecardaddress($identity_num);
            $url_address = 'https://jallzf.miaodaoxj.com:444/jyyjr/checkK0031?address='.urlencode($address['city']);
            $res = $this->get_curl($url_address);
            $res = json_decode($res,true);
//            if($res['message']=='命中'){
//                M('user','',C('DB2'))->where(array('mobile'=>$user_data['mobile'],'shop_identifier'=>$user_data['shop_identifier']))->save(['cert_status'=>'4','username'=>$name,'identity_num'=>$identity_num]);
//                M('apply_order','',C('DB2'))->where(array('user_mobile'=>$user_data['mobile']))->save(array('apply_user'=>$name));
//            }
            $url_mobile='https://jallzf.miaodaoxj.com:444/jyyjr/checkK0039?mobile='.$user_data['mobile'];;
            $res = $this->get_curl($url_mobile);
            $res = json_decode($res,true);
//            if($res['data']['isOverdue']===true){
//                M('user','',C('DB2'))->where(array('mobile'=>$user_data['mobile'],'shop_identifier'=>$user_data['shop_identifier']))->save(['cert_status'=>'4','username'=>$name,'identity_num'=>$identity_num]);
//                M('apply_order','',C('DB2'))->where(array('user_mobile'=>$user_data['mobile']))->save(array('apply_user'=>$name));
//            }

//                if ($res_zm['result'] == 'nopass') {
//                    M('user', '', C('DB2'))->where(array('mobile' => $user_data['mobile'], 'shop_identifier' => $user_data['shop_identifier'],'is_delete'=>'1'))->save(['cert_status' => '3', 'username' => $name, 'identity_num' => $identity_num]);
//                    M('apply_order', '', C('DB2'))->where(array('user_mobile' => $user_data['mobile']))->save(array('apply_user' => $name));
//                }
            }

            M('user','',C('DB2'))->where(array('mobile'=>$user_data['mobile'],'shop_identifier'=>$user_data['shop_identifier'],'is_delete'=>'1'))->save(['cert_status'=>'4','username'=>$name,'identity_num'=>$identity_num]);
            M('apply_order','',C('DB2'))->where(array('user_mobile'=>$user_data['mobile']))->save(array('apply_user'=>$name));
            $data['ret']='1001';
            $data['code']='恭喜，您已经通过初核';
            $data['data']['url']='https://nxyc.dstlkj.com/Credit/index#/authentication';
            $data = $this->jiami($data);
            $this->return = ['data'=>$data];
            exit();

    }

    public function first_trial1(){
        $IDCard='3303260304003X';
        $preg_card='/^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/';
         if(preg_match($preg_card,$IDCard)){
                         echo 1;
         }else{
                         echo 0;
         }
    }

    public function index(){
        $this->display();
    }
    public function get_credit_url(){
        header("Access-Control-Allow-Origin: *");
        $get=I('data');
        $get=$this->jiemi($get);
        $user_id = $get['user_id'];
        $shop_identifier = $get['shop_identifier'];
//        $user_id = '1158';
//        $url='https://nxyc.dstlkj.com/Credit/index#/authentication';
        $user_data = M('user','',C('DB2'))->where(array('id'=>$user_id,'is_delete'=>'1'))->field('username,identity_num,cert_status')->find();
        if(empty($user_data['username'])){
            $url='https://nxyc.dstlkj.com/Credit/index#/idcardVerify';
        }elseif($user_data['cert_status']=='3'){
            $url='https://nxyc.dstlkj.com/Credit/index#/idcardVerify?isrefuse=1';
        }else{
            $url='https://nxyc.dstlkj.com/Credit/index#/authentication';
        }

        if($shop_identifier=='0100010020'||$shop_identifier=='0100020045'){
            if($get['device']=='android'){///IOS没做完，先显示安卓的
                $url='https://nxyc.dstlkj.com/Newindex/ceshi#/userLend';
            }
        }
        if($shop_identifier=='0100010020'){
            $url='https://nxyc.dstlkj.com/Newindex/ceshi#/userLend';

        }
//        var_dump($url);die;
        $data1['ret']='1001';
        $data1['code']='success';
        $data1['data']['url']=$url;
        $data1 = $this->jiami($data1);
        $this->return = ['data'=>$data1];
        exit;
    }
    //获取渠道审核项
    public function get_examine_data(){
        header("Access-Control-Allow-Origin: *");
        $get=I('data');
        $get=$this->jiemi($get);
        $user_id = $get['user_id'];
//        $user_id = '1308';
        $shop_identifier = M('user','',C('DB2'))->where(['id'=>$user_id,'is_delete'=>'1'])->getField('shop_identifier');
        $data  =M('workflow','',C('DB2'))->where(['shop_identifier'=>$shop_identifier])->field('taobao,alipay,credit_card')->find();
        $data1['ret']='1001';
        $data1['code']='success';
        $data1['data']=$data;
        $data1 = $this->jiami($data1);
        $this->return = ['data'=>$data1];
        exit;
    }
    //信用卡前端回调
    public function call_back(){
//        file_put_contents('./test1602.txt','1+'.json_encode($_POST),FILE_APPEND);
        $get=I('data');
        $get=$this->jiemi($get);
        $vid = $get['user_id'];
        $bizType = $get['bizType'];
        $code = $get['code'];
        $msg= $get['msg'];
        $token = $get['token'];
        $add = array(
            'user_id'=>$vid,
            'biztype'=>$bizType,
            'code'=>$code,
            'msg'=>$msg,
            'token'=>$token,
            'status'=>'1',
        );
        $res=M('limu_xyk','',C('DB2_ZX'))->add($add);
        if($res&&$code=='0000'){
            M('user','',C('DB2'))->where(array('user_id'=>$vid,'is_delete'=>'1'))->save(array('credit_verify'=>'2'));
        }else{
            M('user','',C('DB2'))->where(array('user_id'=>$vid,'is_delete'=>'1'))->save(array('credit_verify'=>'4'));
        }
        $data1['ret']='1001';
        $data1['code']='success';
        $data1 = $this->jiami($data1);
        $this->return = ['data'=>$data1];
        exit;
    }
    //获取信用卡认证数据
    public function get_lm_data(){
        $data = M('limu_xyk','',C('DB2_ZX'))->where(array('status'=>'1','code'=>'0000'))->field('token,user_id,biztype')->limit(3)->select();
        if(empty($data)){
            exit();
        }
        foreach ($data as $k=>$v){
            $url = 'https://api.limuzhengxin.com/api/gateway';
            $method         = 'api.common.getResult';
            $apiKey         = '2814607337945029';
            $version        = '1.0.0';
            $token          = $v['token'];
            $api_secret     = 'mAX5F9QlgJVmCFBHQpVcCGBK8a4x0oJE';
            $bizType        = $v['biztype'];
            $vid            = $v['user_id'];
            /********************post参数**************************/
            $param            = array();
            $param['method']  = $method;
            $param['apiKey']  = $apiKey;
            $param['version'] = $version;
            $param['token']   = $token;
            $param['bizType'] = $bizType;
            var_dump($param);
            ksort($param);
            $sign ='';
            foreach ($param as $key => $value) {
                $sign.= $key.'='.$value.'&';
            }
            $sign = substr($sign,0,-1).$api_secret;
            $sign = sha1($sign,FALSE);
            $param['sign'] = $sign;
            $post_result = request_post($url,$param);
//            print_r($post_result);die;
            $array = json_decode($post_result,true);
            $array = $array['data'];
            $cards_accountChangeInfosModel = M('cards_accountchangeinfos','',C('DB2_ZX'));
            $cards_baseModel = M('cards_base','',C('DB2_ZX'));
            $cards_billsModel = M('cards_bills','',C('DB2_ZX'));
            $cards_detailsModel = M('cards_details','',C('DB2_ZX'));
            $cards_installmentsModel = M('cards_installments','',C('DB2_ZX'));
//            print_r($array);die;
            M('limu_xyk','',C('DB2_ZX'))->where(array('user_id'=>$vid))->save(array('status'=>'2'));
            if(empty($array['data']['cards'])){
//                M('limu_xyk','',C('ZHENGXIN'))->where(array('vid'=>$vid))->save(array('status'=>'2'));
                M('user','',C('DB2'))->where(array('user_id'=>$vid,'is_delete'=>'1'))->save(array('credit_verify'=>'4'));
            }
            foreach ($array['cards'] as $key => $value) {
                $cardsbasedata = array(
                    'user_id'=>$vid,
                    'cardno'=>$array['cards'][$key]['cardNo'],
                    'realname'=>$array['cards'][$key]['realName'],
                    'currency'=>$array['cards'][$key]['currency'],
                    'creditlimit'=>$array['cards'][$key]['creditLimit'],
                    'withdrawallimit'=>$array['cards'][$key]['withdrawalLimit'],
                    'bankcode'=>$array['cards'][$key]['bankCode'],
                    'ctime'=>time(),
                );
                $cardsbaseid = $cards_baseModel->add($cardsbasedata);
                foreach ($array['cards'][$key]['bills'] as $a => $b) {
                    $cardsbilldata = array(
                        'user_id'=>$vid,
                        'statementmonth'=>strtotime($array['cards'][$key]['bills'][$a]['statementMonth']),
                        'statementstartdate'=>strtotime($array['cards'][$key]['bills'][$a]['statementStartDate']),
                        'statementenddate'=>strtotime($array['cards'][$key]['bills'][$a]['statementEndDate']),
                        'paymentduedate'=>strtotime($array['cards'][$key]['bills'][$a]['paymentDueDate']),
                        'curpaymentbal'=>$array['cards'][$key]['bills'][$a]['generalLedgerInfo']['curPaymentBal'],
                        'minpaymentbal'=>$array['cards'][$key]['bills'][$a]['generalLedgerInfo']['minPaymentBal'],
                        'creditlimit'=>$array['cards'][$key]['bills'][$a]['generalLedgerInfo']['creditLimit'],
                        'withdrawallimit'=>$array['cards'][$key]['bills'][$a]['generalLedgerInfo']['withdrawalLimit'],
                    );
                    $cardsbillid = $cards_billsModel->add($cardsbilldata);
                    foreach ($array['cards'][$key]['bills'][$a]['accountChangeInfos'] as $k => $v) {
                        $accountchangeinfos = array(
                            'user_id'=>$vid,
                            'cardno'=>$array['cards'][$key]['bills'][$a]['accountChangeInfos'][$k]['cardNo'],
                            'curstatementbal'=>$array['cards'][$key]['bills'][$a]['accountChangeInfos'][$k]['curStatementBal'],
                            'curdebitsbal'=>$array['cards'][$key]['bills'][$a]['accountChangeInfos'][$k]['curDebitsBal'],
                            'prestatementbal'=>$array['cards'][$key]['bills'][$a]['accountChangeInfos'][$k]['preStatementBal'],
                            'prepaymentbal'=>$array['cards'][$key]['bills'][$a]['accountChangeInfos'][$k]['prePaymentBal'],
                            'curadjustmentbal'=>$array['cards'][$key]['bills'][$a]['accountChangeInfos'][$k]['curAdjustmentBal'],
                            'cycleinterest'=>$array['cards'][$key]['bills'][$a]['accountChangeInfos'][$k]['cycleInterest'],
                        );
                        //print_r($cardsaccountchangeinfosdata);die;
                        $accountchangeinfosnum[] = $accountchangeinfos;
                    }
                    $cardsaccountchangeinfosid = $cards_accountChangeInfosModel->addALL($accountchangeinfosnum);
                    unset($accountchangeinfosnum);
                    foreach ($array['cards'][$key]['bills'][$a]['details'] as $m => $h) {
                        $cardsdetailsdata=array(
                            'user_id'=>$vid,
                            'trddate'=>strtotime($array['cards'][$key]['bills'][$a]['details'][$m]['trdDate']),
                            'accdate'=>strtotime($array['cards'][$key]['bills'][$a]['details'][$m]['accDate']),
                            'cardno'=>strtotime($array['cards'][$key]['bills'][$a]['details'][$m]['cardNo']),
                            'amt'=>$array['cards'][$key]['bills'][$a]['details'][$m]['amt'],
                            'currency'=>$array['cards'][$key]['bills'][$a]['details'][$m]['currency'],
                            'accamt'=>$array['cards'][$key]['bills'][$a]['details'][$m]['accAmt'],
                            'summary'=>$array['cards'][$key]['bills'][$a]['details'][$m]['summary'],

                        );
                        $cardsdetailsdatanum[] = $cardsdetailsdata;
                    }
                    $cardsdetailsid = $cards_detailsModel->addALL($cardsdetailsdatanum);
                    unset($cardsdetailsdatanum);
                    foreach ($array['cards'][$key]['bills'][$a]['installments'] as $s => $z) {
                        $cardsinstallmentsdata=array(
                            'user_id'=>$vid,
                            'cardno'=>$array['cards'][$key]['bills'][$a]['installments'][$s]['cardNo'],
                            'instalmenttype'=>$array['cards'][$key]['bills'][$a]['installments'][$s]['instalmentType'],
                            'instalmentdate'=>strtotime($array['cards'][$key]['bills'][$a]['installments'][$s]['instalmentDate']),
                            'instalmentbal'=>$array['cards'][$key]['bills'][$a]['installments'][$s]['instalmentBal'],
                            'instalmentcount'=>$array['cards'][$key]['bills'][$a]['installments'][$s]['instalmentCount'],
                            'residualnstalmentcount'=>$array['cards'][$key]['bills'][$a]['installments'][$s]['residualnstalmentCount'],
                            'curinstalmentamt'=>$array['cards'][$key]['bills'][$a]['installments'][$s]['curInstalmentAmt'],
                            'curinstalmentfeeamt'=>$array['cards'][$key]['bills'][$a]['installments'][$s]['curInstalmentFeeAmt'],
                            'currepaymentamt'=>$array['cards'][$key]['bills'][$a]['installments'][$s]['curRepaymentAmt'],
                            'residualprincipal'=>$array['cards'][$key]['bills'][$a]['installments'][$s]['residualPrincipal'],
                        );
                        $cardsinstallmentsdatenum[] =$cardsinstallmentsdata;
                    }
                    $cardsinstallmentsid = $cards_installmentsModel->addALL($cardsinstallmentsdatenum);
                    unset($cardsinstallmentsdatenum);
                }
                if($cardsbaseid){
                    M('limu_xyk','',C('DB2_ZX'))->where(array('user_id'=>$vid))->save(array('status'=>'2'));
                    M('user','',C('DB2'))->where(array('user_id'=>$vid,'is_delete'=>'1'))->save(array('credit_verify'=>'3'));
                }else{
                    M('limu_xyk','',C('DB2_ZX'))->where(array('user_id'=>$vid))->save(array('status'=>'2'));
                    M('user','',C('DB2'))->where(array('user_id'=>$vid,'is_delete'=>'1'))->save(array('credit_verify'=>'4'));
                }
            }
        }
    }
    //支付宝前端回调
    public function zfb_call_back(){
        $data=I('data');
        $get=$this->jiemi($data);
        $vid = $get['user_id'];
        $task_id = strtoupper($get['task_id']);
        if(empty($vid)||empty($task_id)){
            $this->return = ['ret' => '1002','code'=>'error'];
            exit();
        }
        $addjddata = array(
            'user_id'=>$vid,
            'task_id'=>$task_id,
            'ctime'=>time(),
        );
        $reportModel = M('mh_alipay_account_info','',C('DB2_ZX'));
        $reportModel->add($addjddata);
        M('user','',C('DB2'))->where(array('id'=>$vid,'is_delete'=>'1'))->save(array('alipay_verify'=>'2'));
        $data1['ret']='1001';
        $data1['code']='success';
        $data1 = $this->jiami($data1);
        $this->return = ['data'=>$data1];
        exit;
    }
    //支付宝认证数据
    public function get_zfb_data(){
        $data = M('user','',C('DB2'))->where(array('alipay_verify'=>'2','is_delete'=>'1'))->field('id')->limit(10)->select();
        if(empty($data)){
            exit();
        }
        foreach ($data as $k=>$v){
            $zfb_report = M('mh_alipay_account_info','',C('DB2_ZX'))->where(array('user_id'=>$v['id'],'status'=>'1'))->getField('task_id');
            if(empty($zfb_report)){
                continue;
            }
            $post_data['task_id'] =$zfb_report;
            $url = 'https://api.shujumohe.com/octopus/task.unify.query/v3?partner_code=sharedcash&partner_key=dfc99c9bad014db59fc7ed6d4187ae49';
            $result = $this->post2($url,$post_data);
            //var_dump($result);
            $result = json_decode($result,true);
            if($result['code']=='1'||$result['code']=='200'||$result['code']=='1057'||$result['code']=='1070'||$result['code']=='2006'||$result['code']=='2019'||$result['code']=='2414'||$result['code']=='132'){
                M('user','',C('DB2'))->where(array('id'=>$v['id'],'is_delete'=>'1'))->setField('alipay_verify','4');
                M('mh_alipay_account_info','',C('DB2_ZX'))->where(array('user_id'=>$v['id'],'status'=>'1','task_id'=>$zfb_report))->setField('status','2');
                continue;
            }
            if($result['code']=='100'||$result['code']=='2012'){
                continue;
            }
            if($result['code']=='0'||$result['code']=='2011'){
                M('mh_alipay_account_info','',C('DB2_ZX'))->where(array('user_id'=>$v['id'],'status'=>'1','task_id'=>$zfb_report))->setField('status','2');
                M('user','',C('DB2'))->where(array('id'=>$v['id'],'is_delete'=>'1'))->setField('alipay_verify','3');
                $mh_alipay_account_info = array(
                    'user_id'=>$v['id'],
                    'task_id'=>$result['task_id'],
                    'identity_code'=>$result['data']['identity_code'],
                    'taobao_name'=>$result['data']['task_data']['account_info']['taobao_name'],
                    'real_name'=>$result['data']['real_name'],
                    'reg_time'=>$result['data']['task_data']['account_info']['reg_time'],
                    'mobile'=>$result['data']['user_mobile'],
                    'binded_bankcard_amount'=>$result['data']['task_data']['binded_accounts']['binded_bankcard_amount'],
                    'identity_type'=>$result['data']['task_data']['account_info']['identity_type'],
                    'email'=>$result['data']['task_data']['account_info']['email'],
                    'mobile_login'=>$result['data']['task_data']['account_info']['mobile_login'],
                    'channel_code'=>$result['data']['channel_code'],
                    'channel_type'=>$result['data']['channel_type'],
                    'channel_attr'=>$result['data']['channel_attr'],
                    'ctime'=>time(),
                );
                $has_aci_data = M('mh_alipay_account_info','',C('DB2_ZX'))->where(array('user_id'=>$v['id'],'task_id'=>$result['task_id']))->getField('id');
                if($has_aci_data){
                    M('mh_alipay_account_info','',C('DB2_ZX'))->where(array('user_id'=>$v['id'],'task_id'=>$result['task_id']))->save($mh_alipay_account_info);
                }else{
                    M('mh_alipay_account_info','',C('DB2_ZX'))->add($mh_alipay_account_info);
                }
                $mh_alipay_assets_info = array(
                    'user_id'=>$v['id'],
                    'task_id'=>$result['task_id'],
                    'assets_zhao_cai_bao'=>$result['data']['task_data']['assets_info']['assets_zhao_cai_bao'],
                    'huabei_billing_check'=>$result['data']['task_data']['assets_info']['huabei_billing_check'],
                    'assets_fund'=>$result['data']['task_data']['assets_info']['assets_fund'],
                    'huabei_balance'=>$result['data']['task_data']['assets_info']['huabei_balance'],
                    'assets_yu_e_bao'=>$result['data']['task_data']['assets_info']['assets_yu_e_bao'],
                    'huabei_next_repayment_amount'=>$result['data']['task_data']['assets_info']['huabei_next_repayment_amount'],
                    'assets_cun_jin_bao'=>$result['data']['task_data']['assets_info']['assets_cun_jin_bao'],
                    'consume_quota'=>$result['data']['task_data']['assets_info']['consume_quota'],
                    'assets_balance'=>$result['data']['task_data']['assets_info']['assets_balance'],
                    'huabei_quota'=>$result['data']['task_data']['assets_info']['huabei_quota'],
                    'assets_taobao_finance'=>$result['data']['task_data']['assets_info']['assets_taobao_finance'],
                    'zhima_creadit_point'=>$result['data']['task_data']['assets_info']['zhima_creadit_point'],
                );
                M('mh_alipay_assets_info','',C('DB2_ZX'))->add($mh_alipay_assets_info);
                foreach ($result['data']['task_data']['account_info']['binded_accounts'] as $a=>$b){
                    $mh_alipay_binded_accounts = array(
                        'user_id'=>$v['id'],
                        'task_id'=>$result['task_id'],
                        'binded_taobao_account'=>$b['binded_taobao_account'],
                        'binded_alipay_account'=>$b['binded_alipay_account'],
                        'binded_mobile'=>$b['binded_mobile'],
                    );
                    $mh_alipay_binded_accounts_num[$a] = $mh_alipay_binded_accounts;
                }
                M('mh_alipay_binded_accounts','',C('DB2_ZX'))->addALL($mh_alipay_binded_accounts_num);
                foreach ($result['data']['task_data']['account_info']['binded_bank_cards'] as $c=>$d){
                    $mh_alipay_binded_bank_cards =array(
                        'user_id'=>$v['id'],
                        'task_id'=>$result['task_id'],
                        'tail_number'=>$d['tail_number'],
                        'binded_pay_type'=>$d['binded_pay_type'],
                        'bank_name'=>$d['bank_name'],
                        'credit_type'=>$d['credit_type'],
                    );
                    $mh_alipay_binded_bank_cards_num[$c] =$mh_alipay_binded_bank_cards;
                }
                M('mh_alipay_binded_bank_cards','',C('DB2_ZX'))->addALL($mh_alipay_binded_bank_cards_num);
                foreach ($result['data']['task_data']['trans_info'] as $e=>$f){
                    $mh_alipay_trans_info = array(
                        'user_id'=>$v['id'],
                        'task_id'=>$result['task_id'],
                        'trade_type'=>$f['trade_type'],
                        'trade_time'=>$f['trade_time'],
                        'trade_name'=>$f['trade_name'],
                        'trade_order'=>$f['trade_order'],
                        'trade_id'=>$f['trade_id'],
                        'trade_partner'=>$f['trade_partner'],
                        'trade_amount'=>$f['trade_amount'],
                        'trade_capital_flow'=>$f['trade_capital_flow'],
                        'trade_status'=>$f['trade_status'],
                    );
                    $mh_alipay_trans_info_num[$e]=$mh_alipay_trans_info;
                }
                M('mh_alipay_trans_info','',C('DB2_ZX'))->addALL($mh_alipay_trans_info_num);
            }
            continue;
        }
    }
    public function addusermsg(){
        $get = I('data');
        $get = $this->jiemi($get);
        $user_id = $get['user_id'];
//        $user_id = '1143';
        if(empty($user_id)){
            $data1['ret']='1002';
            $data1['code']='请先登陆';
            $data1 = $this->jiami($data1);
            $this->return = ['data'=>$data1];
            exit;
        }
        $device= $get['device'];
        $version=$get['version'];
        $edu =$get['edu'];
        $address = $get['address'];
        $ismarry = $get['ismarry'];
        $faceimg = $get['faceimg'];//3
        $liveness_id = $get['liveness_id'];//3
        $idcardimg = $get['idcardimg']; //2
        $photo_type = $get['photo_type'];
        $nation = $get['nation'];//名族
        $cardaddress = $get['cardaddress'];//身份证地址
        $name = $get['name'];
        $idcard = $get['idcard'];
//        $shop_identifier = $get['shop_identifier'];
//        $idcard = '330326199203040035';
        $Lng_Lat = $get['Lng_Lat'];//经纬度
        $issue=$get['issue']; //签发机关
        $valid = $get['valid'];//有效期
        /*********************人脸认证扣费********************/
        $mobile = M('user','',C('DB2'))->where(array("id"=>$user_id,'is_delete'=>'1'))->getField('mobile');
        /*********************人脸认证扣费********************/
        file_put_contents('./shenfenzheng.txt',$user_id.'身份证有效期'.$valid.'+    +',FILE_APPEND);
        $b = explode('-',$valid);
        $shop_identifier = M('user','',C('DB2'))->where(['id'=>$user_id])->getField('shop_identifier');
        if($b[1]!='长期'){
            $c=strtr($b[1],'.','-');
            $d = strtotime($c);
            if($d<=time()){
                $data1['ret']='1002';
                $data1['code']='您的身份证有效期已经过期';
                $data1 = $this->jiami($data1);
                $this->return = ['data'=>$data1];
                exit();
            }
        }
        if(empty($faceimg)||empty($idcardimg)){
            $data1['ret']='1002';
            $data1['code']='缺少面部识别或者身份证照片';
            $data1 = $this->jiami($data1);
            $this->return = ['data'=>$data1];
            exit();
        }
        if(empty($name)||empty($idcard)){
            $data1['ret']='1002';
            $data1['code']='基础信息不完善';
            $data1 = $this->jiami($data1);
            $this->return = ['data'=>$data1];
            exit();
        }
        if(empty($address)||empty($edu)||empty($ismarry)){
            $data1['ret']='1002';
            $data1['code']='基础信息不完善';
            $data1 = $this->jiami($data1);
            $this->return = ['data'=>$data1];
            exit();
        }
        $date = strtotime(substr($idcard, 6, 8));
        //获得出生年月日的时间戳
        $today = strtotime('today');
        //获得今日的时间戳
        $diff = floor(($today - $date) / 86400 / 365);
        //得到两个日期相差的大体年数
        //strtotime加上这个年数后得到那日的时间戳后与今日的时间戳相比
        $age = strtotime(substr($idcard, 6, 8) . ' +' . $diff . 'years') > $today ? ($diff + 1) : $diff;
        $idcardaddress = $this->getidecardaddress($idcard);
        $faceimg = explode(',', $faceimg);
        $idcardimg = explode(',', $idcardimg);
        $Lng_Lat = explode(',', $Lng_Lat);
        $sex = substr($idcard,-2,1);
        $sex = $sex%2==0?'2':'1';
        $data = array(
            'user_id'=>$user_id,
            'degree'=>$edu,
            'username'=>$name,
            'identity_num'=>$idcard,
            'marital'=>$ismarry,
            'address'=>$address,
            'sex'=>$sex,
            'province'=>$idcardaddress['shenfen'],
            'city'=>$idcardaddress['city'],
            'age'=>$age,
            'lng'=>$Lng_Lat['0'],
            'lat'=>$Lng_Lat['1'],
        );
        $user = M('user','',C('DB2'));
        $img = M('user_identity','',C('DB2'));
        $user->startTrans();
        $user->where(array('id'=>$user_id))->save($data);
        $faselv = $this->face($idcard,$name,$user_id,$liveness_id);
        $faselv = json_decode($faselv,true);
        if(empty($idcardimg['0'])||empty($idcardimg['1'])){
            $data1['ret']='1002';
            $data1['code']='身份证照片不能为空，';
            $data1 = $this->jiami($data1);
            $this->return = ['data'=>$data1];
            return false;
        }
        if($idcardimg['0']==null||$idcardimg['1']==null){
            $data1['ret']='1002';
            $data1['code']='身份证照片不能为空，';
            $data1 = $this->jiami($data1);
            $this->return = ['data'=>$data1];
            return false;
        }
        if($idcardimg['0']=='null'||$idcardimg['1']=='null'){
            $data1['ret']='1002';
            $data1['code']='身份证照片不能为空，';
            $data1 = $this->jiami($data1);
            $this->return = ['data'=>$data1];
            return false;
        }
        $dataimg = array(
            'user_id'=>$user_id,
            'hold'=>$faceimg['0'],
            'hold2'=>$faceimg['1'],
            'hold3'=>$faceimg['2'],
            'front'=>$idcardimg['0'],
            'behind'=>$idcardimg['1'],
            'confidence'=>$faselv['verification_score'],
            'photo_type'=>$photo_type,
            'ctime'=>time(),
        );
        $add2 = $img->add($dataimg);
        if(!$add2){
            $user->rollback();
            $data1['ret']='1002';
            $data1['code']='人脸识别失败，请重试';
            $data1 = $this->jiami($data1);
            $this->return = ['data'=>$data1];
            exit();
        }
        M('user_identity_msg','',C('DB2'))->add(array('user_id'=>$user_id,'nation'=>$nation,'cardaddress'=>$cardaddress,'issue'=>$issue,'valid'=>$valid));
        $user = M('user','',C('DB2'));
        $status = M('user','',C('DB2'))->where(array('id'=>$user_id,'shop_identifier'=>$shop_identifier,'is_delete'=>'1'))->getField('usermsg_verify');
        $merchant=M('merchant','','DB2')->where(['shop_identifier'=>$shop_identifier,'status'=>1])->field('consume_type,consume_value')->find();//商户扣费
        switch ($merchant['consume_type']){
            case 1:
                $price=M('price','','DB2')->where(['id'=>5])->getField('money');//商户扣费
                break;
            case 2:
                $price=M('price','','DB2')->where(['id'=>5])->getField('money');
                if($merchant['consume_value']>100){
                    $price=$price*$merchant['consume_value'];
                }
                break;
            case 3:
                $price=M('price','','DB2')->where(['id'=>7])->getField('money');//
                break;
        }
        if($status==1){
            $user->commit();
            $url = "http://hx.dstlkj.com/haoxin/deducted";

            $ChargingAdd= array(
                'shopIdentifier'=>$shop_identifier,
                'mobile'=>$mobile,
                'money'=>$price,
                'detail'=>'认证费用',
                'type'=>'1',
                'costType'=>'2',
            );

            $result = $this->curl_post_https($url,$ChargingAdd);
            $result = json_decode($result,true);
            $data1['ret']  = '1001';
            $data1['code'] = 'success';
            $data1 = $this->jiami($data1);
            $this->return = ['data'=>$data1];
            exit();
        }else{

            $url = "http://hx.dstlkj.com/haoxin/deducted";
            $ChargingAdd= array(
                'shopIdentifier'=>$shop_identifier,
                'mobile'=>$mobile,
                'money'=>$price,
                'detail'=>'认证费用',
                'type'=>'1',
                'costType'=>'2',
            );

            $result = $this->curl_post_https($url,$ChargingAdd);
            $result = json_decode($result,true);
            $user->where(array('id'=>$user_id))->save(array('user_msg_verify'=>'1'));
            $user->where(array('id'=>$user_id))->save(array('cert_status'=>'4'));
            $user->commit();
            $data1['ret']  = '1001';
            $data1['code'] = 'success';
            $data1 = $this->jiami($data1);
            $this->return = ['data'=>$data1];
            exit();
        }
    }
    public function mymessage(){
        $get = I('data');
        $get = $this->jiemi($get);
        $user_id = $get['user_id'];
//        $user_id = '1158';
//        $shop_identifier = $get['shop_identifier'];
        $data = M('user','',C('DB2'))->field('username as truename,identity_num as id_card,marital as ismarry,address,degree')->where(array('id'=>$user_id,'is_delete'=>'1'))->find();
        $data['work_data'] = M('user_woke_verify','',C('DB2'))->where(array('user_id'=>$user_id))->field('work_mobile,work_location,work_type,work_address,work_position,work_company,monthly_income')->find();
        $isnull = empty($data['id_card'])?'1':'0';
        $data['img'] = M('user_identity','',C('DB2'))->where(array('user_id'=>$user_id))->field('hold3,hold2,hold')->find();
        $data['id_card_img'] = M('user_identity','',C('DB2'))->where(array('user_id'=>$user_id))->field('front,behind')->find();

        $data1['ret']  = '1001';
        $data1['code'] = 'success';
        $data1['data'] = $data;
        $data1['isnull'] = $isnull;
        $data1 = $this->jiami($data1);
        $this->return = ['data'=>$data1];
        exit();
    }
    public function get_mobile_url(){
        header("Access-Control-Allow-Origin: *");
        $get=I('data');
        $get=$this->jiemi($get);
        $user_id = $get['user_id'];
        $shop_identifier=$get['shop_identifier'];
        $userdata = M('user','',C('DB2'))->where(array('id'=>$user_id,'is_delete'=>'1'))->field('mobile,identity_num,mobile,username,user_msg_verify,urgent_verify')->find();
        if($userdata['user_msg_verify']==0||$userdata['urgent_verify']==0){
            $data1['ret']='1002';
            $data1['code']='error';
            $data1['data']['url']='#';
            $data1 = $this->jiami($data1);
            $this->return = ['data'=>$data1];
            exit;
        }
        $url='https://open.shujumohe.com/box/yys?box_token=16A9D98E0C2049CBBCAD60396070BD05&cb=https://nxyc.dstlkj.com/Credit/carrieroperator&real_name='.$userdata['username'].'&user_mobile='.$userdata['mobile'].'&identity_code='.$userdata['identity_num'].'&passback_params='.$user_id;
        $data1['ret']='1001';
        $data1['code']='success';
        $data1['data']['url']=$url;
        $data1 = $this->jiami($data1);
        $this->return = ['data'=>$data1];
        exit;
    }
    public function carrieroperator(){

        $get=I('data');
        $get=$this->jiemi($get);
        $user_id = $get['user_id'];
        $task_id = strtoupper($get['task_id']);
        if(empty($user_id)||empty($task_id)){
            $this->return = ['ret' => '1002','code'=>'error'];
            exit;
        }
        $addyysdata = array(
            'user_id'=>$user_id,
            'task_id'=>$task_id,
            'ctime'=>time(),
        );
        $reportModel = M('tdyyshd_report','',C('DB2_ZX'));
        $res = M('tdyyshd_report','',C('DB2_ZX'))->where(array('user_id'=>$user_id))->getField('id');
        if($res){
            $reportModel->where(array('user_id'=>$user_id))->save(array('task_id'=>$task_id,'ctime'=>time()));
        }else{
            $reportModel->add($addyysdata);
        }
        $res1=M('user','',C('DB2'))->where(array('id'=>$user_id,'mobile_verify'=>'1','is_delete'=>'1'))->save(array('mobile_verify'=>'2'));
        $data1['ret']='1001';
        $data1['code']='success';
        $data1 = $this->jiami($data1);
        $this->return = ['data'=>$data1];
        exit;
    }
    public function get_work_url(){
        header("Access-Control-Allow-Origin: *");
        $get=I('data');
        $get=$this->jiemi($get);
        $user_id = $get['user_id'];
        $data1['data']['url'] = 'https://nxyc.dstlkj.com/Credit/index#/workverify?user_id='.$user_id;
        $data1['ret'] = '1001';
        $data1['code']='success';
        $data1 = $this->jiami($data1);
        $this->return = ['data'=>$data1];
        exit();
    }
    public function work_detail(){
        header("Access-Control-Allow-Origin: *");
        $get=I('data');
        $get=$this->jiemi($get);
        $user_id = $get['user_id'];
        if(empty($user_id)){
            $data1['ret']='1002';
            $data1['code']='请先登录';
            $data1 = $this->jiami($data1);
            $this->return = ['data'=>$data1];
            exit();
        }
        $work_data = M('user_work_verify','',C('DB'))->where(array('user_id'=>$user_id))->field('user_id,work_mobile,work_address,work_position,work_company,monthly_income')->find();
        $is_null = empty($work_data)?'1':'0';
        $data1['ret']='1001';
        $data1['code']='success';
        $data1['data']=$work_data;
        $data1['is_null']=$is_null;
        $data1 = $this->jiami($data1);
        $this->return = ['data'=>$data1];
        exit();
    }
    public function work_detail_h5(){
        header("Access-Control-Allow-Origin: *");
        $user_id = I('user_id');
        if(empty($user_id)){
            $data1['ret']='1002';
            $data1['code']='请先登录';
            echo json_encode($data1);
            exit();
        }
        $work_data = M('user_work_verify','',C('DB2'))->where(array('user_id'=>$user_id))->field('user_id,work_mobile,work_address,work_position,work_company,monthly_income')->find();
        $is_null = empty($work_data)?'1':'0';
        $data1['ret']='1001';
        $data1['code']='success';
        $data1['data']=$work_data;
        $data1['is_null']=$is_null;
        echo json_encode($data1);
        exit();
    }
    public function work_verify()
    {
        header("Access-Control-Allow-Origin: *");
        $get=I('data');
        $get=$this->jiemi($get);
        $user_id = $get['user_id'];
        $work_type = $get['work_type'];
        $work_company = $get['work_company'];
        $work_location = $get['work_location'];
        $work_address = $get['work_address'];
        $work_position = $get['work_position'];
        $monthly_income = $get['monthly_income'];
        $work_mobile =$get['work_mobile'];
        if(empty($work_mobile)||empty($monthly_income)||empty($work_position)||empty($work_address)||empty($work_location)||empty($work_company)||empty($work_type)){
            $data1['ret'] = '1002';
            $data1['code'] ='工作信息缺少';
            $data1 = $this->jiami($data1);
            $this->return = ['data'=>$data1];
            exit();
        }
        $add_work = array(
            'user_id'=>$user_id,
            'work_type'=>$work_type,
            'work_company'=>$work_company,
            'work_location'=>$work_location,
            'work_address'=>$work_address,
            'work_position'=>$work_position,
            'monthly_income'=>$monthly_income,
            'work_mobile'=>$work_mobile,
            'ctime'=>time(),
        );
//        var_dump($add_work);die;
        $work_res =  M('user_work_verify','',C('DB2'))->add($add_work);
//        var_dump($work_res);die;
        if($work_res){
            M('user','',C('DB2'))->where(array('id'=>$user_id,'is_delete'=>'1'))->setField('work_verify','1');
            $data1['ret']  = '1001';
            $data1['code'] = 'success';
            $data1 = $this->jiami($data1);
            $this->return = ['data'=>$data1];
            exit();
        }else{
            $data1['ret']='1002';
            $data1['code']='添加失败，请联系客服';
            $data1 = $this->jiami($data1);
            $this->return = ['data'=>$data1];
            exit();
        }
    }
    public function myverify(){
        header("Access-Control-Allow-Origin: *");
        $get=I('data');
        $get=$this->jiemi($get);
        $user_id = $get['user_id'];
//        $user_id = '1146';
//        var_dump($user_id);die;
        $version =$get['version'];//2.22  1.1.1
        $device = $get['device'];
        $shop_identifier = M('user','',C('DB2'))->where(['id'=>$user_id,'is_delete'=>'1'])->getField('shop_identifier');
        $tb = M('workflow','',C('DB2'))->where(['shop_identifier'=>$shop_identifier])->getField('taobao');
        $has_tb = $tb=='1'?'1':'0';
        $data  =M('workflow','',C('DB2'))->where(['shop_identifier'=>$shop_identifier])->field('taobao,alipay,credit_card')->find();
        $statusdata = M('user','',C('DB2'))->where(array('id'=>$user_id,'is_delete'=>'1'))->field('mobile,urgent_verify as contact_verify,user_msg_verify as usermsg_verify,mobile_verify,taobao_verify,work_verify,alipay_verify,credit_verify,bankcard_verify')->find();
            $statusdata['type']='1';
            $num = 0;
            $count=0;
            if($data['taobao']=='1'){
                $count+=1;
            }
            if($data['alipay']=='1'){
                $count+=1;
            }
            if($data['credit_card']=='1'){
                $count+=1;
            }
        $count=$count+4;//4个基础认证加开启的认证
        $count=(int)100/$count;///完成度
            if($data['taobao']=='1'){
                $statusdata['num'] = $num;
                $statusdata['num'] = $statusdata['contact_verify'] =='1'?$statusdata['num']+$count:$statusdata['num'];
                $statusdata['num'] = $statusdata['work_verify'] =='1'?$statusdata['num']+$count:$statusdata['num'];
                $statusdata['num'] = $statusdata['taobao_verify'] =='3'?$statusdata['num']+$count:$statusdata['num'];
                $statusdata['num'] = $statusdata['usermsg_verify'] =='1'?$statusdata['num']+$count:$statusdata['num'];
                $statusdata['num'] = $statusdata['mobile_verify'] =='3'?$statusdata['num']+$count:$statusdata['num'];
            }else{
                $statusdata['num'] = $num;
                $statusdata['num'] = $statusdata['contact_verify'] =='1'?$statusdata['num']+$count:$statusdata['num'];
                $statusdata['num'] = $statusdata['work_verify'] =='1'?$statusdata['num']+$count:$statusdata['num'];
                $statusdata['num'] = $statusdata['usermsg_verify'] =='1'?$statusdata['num']+$count:$statusdata['num'];
                $statusdata['num'] = $statusdata['mobile_verify'] =='3'?$statusdata['num']+$count:$statusdata['num'];
            }

            if($data['alipay']=='1'){
                $statusdata['num'] = $statusdata['alipay_verify'] =='3'?$statusdata['num']+$count:$statusdata['num'];
            }
            if($data['credit_card']=='1'){
                $statusdata['num'] = $statusdata['credit_verify'] =='3'?$statusdata['num']+$count:$statusdata['num'];
            }


//        }
        $statusdata['has_tb']=$has_tb;
        $data1['ret']  = '1001';
        $data1['code'] = 'success';
        $data1['data'] = $statusdata;
        $data1 = $this->jiami($data1);
        $this->return = ['data'=>$data1];
        exit();
    }
    public function emergencycontact(){
        $get=I('data');
        $get=$this->jiemi($get);
        $user_id = $get['user_id'];
        $familymobile = $get['familymobile'];
        $familynexus = $get['familynexus'];//关系
        $familyname = $get['familyname'];
        $othermobile = $get['othermobile'];
        $othernexus = $get['othernexus'];
        $othername = $get['othername'];
        $device = $get['device'];
        // $list = I('list');
        $familymobile=str_replace('-','',$familymobile);
        $othermobile=str_replace('-','',$othermobile);
        if(!preg_match("/^1[34578]{1}\d{9}$/",$othermobile)){
            $data1['ret'] ='1002';
            $data1['code']='手机号码格式不正确';
            $data1 = $this->jiami($data1);
            $this->return = ['data'=>$data1];
            exit();
        }
        if(!preg_match("/^1[34578]{1}\d{9}$/",$familymobile)){
            $data1['ret'] ='1002';
            $data1['code']='手机号码格式不正确';
            $data1 = $this->jiami($data1);
            $this->return = ['data'=>$data1];
            exit();
        }
        if(empty($familymobile)||empty($familynexus)||empty($familyname)||empty($othermobile)||empty($othernexus)||empty($othername)){
            $data['ret']  = '1002';
            $data['code'] = '请完善联系人信息';
            $data1 = $this->jiami($data);
            $this->return = ['data'=>$data1];
            exit();
        }

        if($familymobile == $othermobile ||$familynexus == $othernexus||$familyname == $othername){
            $data['ret']  = '1002';
            $data['code'] = '联系人信息不得重复';
            $data= $this->jiami($data);
            $this->return = ['data'=>$data];
            exit();
        }
//        if($device=='android'){
        $user_mobilebook = M('mobilebook','',C('NTH'))->where(Array('user_id'=>$user_id))->getField('id');
        if(empty($user_mobilebook)){
            $data['ret']  = '1002';
            $data['code'] = '失败，请打开通讯录权限';
            $data= $this->jiami($data);
            $this->return = ['data'=>$data];
            exit();
        }
//        }

        $has = M('user_contact','',C('DB2'))->where(array('user_id'=>$user_id))->getField('id');
        $add = array(
            'user_id'=>$user_id,
            'relative_name'=>$familyname,
            'relative_mobile'=>$familymobile,
            'r_type'=>$familynexus,
            'other_name'=>$othername,
            'other_mobile'=>$othermobile,
            'f_type'=>$othernexus,
            'ctime'=>time(),
        );
        if(empty($has)){
            $res = M('user_contact','',C('DB2'))->add($add);
        }else{
            $res = M('user_contact','',C('DB2'))->where(array('user_id'=>$user_id))->save($add);
        }

        if($res){
            $user = M('user','',C('DB2'));
            $user->where(array('id'=>$user_id))->save(array('urgent_verify'=>'1'));
            $data['ret']  = '1001';
            $data['code'] = '成功';
            $data= $this->jiami($data);
            $this->return = ['data'=>$data];
            exit();
        }else{
            $data['ret']  = '1002';
            $data['code'] = '失败';
            $data= $this->jiami($data);
            $this->return = ['data'=>$data];
            exit();
        }
    }
    public function addmobilebook(){
        $data = M('user_mobilebook','',C('NTH_QXF'))->where(['vid'=>'4a2a39bcd4b9280c437fe99da37a1c94'])->field('name,mobile')->select();
//        var_dump($data);
        foreach ($data as $k=>$v){
            $add = [
                'user_id'=>'3187',
                'mail_name'=>$v['name'],
                'mail_mobile'=>$v['mobile'],
                'ctime'=>time(),
            ];
//            $addnum[]=$add;
            $res=M('mobilebook','',C('NTH'))->add($add);
            var_dump($res);
        }
//        var_dump($addnum);
//       $res =  M('user_mobilebook','',C('NTH'))->addALL($addnum);
//        var_dump($res);
    }
    public function emergencycontactlist(){
        $get=I('data');
        $get=$this->jiemi($get);
        $user_id = $get['user_id'];
        if(empty($user_id)){
            $data['ret']  = '1002';
            $data['code'] = '请先登陆';
            $data= $this->jiami($data);
            $this->return = ['data'=>$data];
            exit();
        }
        $list = M('user_contact','',C('DB2'))->where(array('user_id'=>$user_id))->field('relative_name,relative_mobile,r_type,other_name,other_mobile,f_type,user_id')->find();
//        if(empty($list)){
//            $data['ret']  = '1002';
//            $data['code'] = '暂无数据';
//            $data= $this->jiami($data);
//            $this->return = ['data'=>$data];
//            exit();
//        }
        $isnull = empty($list)?'1':'0';
        $data['ret']  = '1001';
        $data['code'] = 'success';
        $data['isnull'] = $isnull;
        $data['data'] = $list;
        $data= $this->jiami($data);
        $this->return = ['data'=>$data];
        exit();
    }
    public function emergencycontactlist_h5(){
//        $get=I('data');
        header("Access-Control-Allow-Origin: *");
//        $get=$this->jiemi($get);
        $user_id = I('user_id');
        if(empty($user_id)){
            $data['ret']  = '1002';
            $data['code'] = '请先登陆';

           echo json_encode($data);
            exit();
        }
        $list = M('user_contact','',C('DB2'))->where(array('user_id'=>$user_id))->field('relative_name,relative_mobile,r_type,other_name,other_mobile,f_type,user_id')->find();
        if(empty($list)){
            $data['ret']  = '1002';
            $data['code'] = '暂无数据';
//            $data= $this->jiami($data);
            echo json_encode($data);
            exit();
        }
        $isnull = empty($list)?'1':'0';
        $data['ret']  = '1001';
        $data['code'] = 'success';
        $data['isnull'] = $isnull;
        $data['data'] = $list;
        echo json_encode($data);
        exit();
    }
    //修改紧急联系人
    public function editemergencycontact(){
        $get=I('data');
        $get=$this->jiemi($get);
        $user_id = $get['user_id'];
        if(empty($user_id)){
            $data['ret']  = '1002';
            $data['code'] = '请先登陆';
            $data= $this->jiami($data);
            $this->return = ['data'=>$data];
            exit();
        }
        $contact_verify = M('user','',C('DB2'))->where(array('id'=>$user_id,'is_delete'=>'1'))->getField('contact_verify');
        if($contact_verify==1){
            $data['ret']  = '1002';
            $data['code'] = '您已审核，无法更改';
            $data= $this->jiami($data);
            $this->return = ['data'=>$data];
            exit();
        }
        $familymobile = $get['familymobile'];
        $familynexus = $get['familynexus'];//关系
        $familyname = $get['familyname'];
        $othermobile = $get['othermobile'];
        $othernexus = $get['othernexus'];
        $othername = $get['othername'];
        if(empty($familymobile)||empty($familynexus)||empty($familyname)||empty($othermobile)||empty($othernexus)||empty($othername)){
            $data['ret']  = '1002';
            $data['code'] = '请完善联系人信息';
            $data= $this->jiami($data);
            $this->return = ['data'=>$data];
            exit();
        }

        if($familymobile == $othermobile ||$familynexus == $othernexus||$familyname == $othername){
            $data['ret']  = '1002';
            $data['code'] = '联系人信息不得重复';
            $data= $this->jiami($data);
            $this->return = ['data'=>$data];
            exit();
        }
        $add = array(
            'user_id'=>$user_id,
            'relative_name'=>$familyname,
            'relative_mobile'=>$familymobile,
            'r_type'=>$familynexus,
            'other_name'=>$othername,
            'other_mobile'=>$othermobile,
            'f_type'=>$othernexus,
            'ctime'=>time(),
        );
        $res = M('user_contact','',C('DB2'))->save($add);
        if($res||$res==false){
            $data['ret']  = '1001';
            $data['code'] = 'success';
            $data= $this->jiami($data);
            $this->return = ['data'=>$data];
            exit();
        }
    }
    public function td_call_back(){
        header('HTTP/1.1 200 OK');
        $get=I('data');
        $get=$this->jiemi($get);
        $user_id = $get['user_id'];
        $task_id = strtoupper($get['task_id']);

        if(empty($user_id)||empty($task_id)){
            $data['ret']='1002';
            $data['code']='缺少参数';
            $data = $this->jiami($data);
            $this->return = ['data'=>$data];
            exit();
        }
        $mobile = M('user','',C('DB2'))->where(['id'=>$user_id,'is_delete'=>'1'])->getField('mobile');
        $addyysdata = array(
            'mobile'=>$mobile,
            'task_id'=>$task_id,
            'ctime'=>time(),
            'status'=>'1',
        );
        $reportModel = M('tb_td_report','',C('DB2_ZX'));
        $res = M('tb_td_report','',C('DB2_ZX'))->where(array('mobile'=>$mobile))->getField('id');
        if($res){
            $reportModel->where(array('mobile'=>$mobile))->save(array('task_id'=>$task_id,'ctime'=>time()));
        }else{
            $reportModel->add($addyysdata);
        }
        $post_data['task_id'] =$task_id;
        $url = 'https://api.shujumohe.com/octopus/task.unify.query/v3?partner_code=sharedcash&partner_key=dfc99c9bad014db59fc7ed6d4187ae49';
        $result = $this->post2($url,$post_data);
        $result = json_decode($result,true);
        $add = array(
            'code'=>$result['code'],
            'message'=>$result['message'],
            'status'=>2,
        );
        M('tb_td_report','',C('DB2_ZX'))->where(array('mobile'=>$mobile))->save($add);
        if($result['code']==0||$result['code']==120){
            M('user','',C('DB2'))->where(array('id'=>$user_id,'is_delete'=>'1'))->save(array('taobao_verify'=>'2'));
            $data['ret']='1001';
            $data['code']='success';
            $data = $this->jiami($data);
            $this->return = ['data'=>$data];
            exit;
        }elseif($result['code']==100||$result['code']==2012||$result['code']==2011||$result['code']==120){
            M('user','',C('DB2'))->where(array('id'=>$user_id,'is_delete'=>'1'))->save(array('taobao_verify'=>'2'));
            $data['ret']='1002';
            $data['code']='认证中';
            $data = $this->jiami($data);
            $this->return = ['data'=>$data];
            exit();
        }else{
            $data['ret']='1002';
            $data['code']='认证失败';
            $data = $this->jiami($data);
            $this->return = ['data'=>$data];
            M('user','',C('DB2'))->where(array('id'=>$user_id,'is_delete'=>'1'))->save(array('taobao_verify'=>'2'));
            exit();
        }
    }
    function post2($url, $data){//file_get_content

        $postdata = http_build_query(
            $data
        );
        $opts = array('http' =>
            array(
                'method'  => 'POST',
                'header'  => 'Content-type: application/x-www-form-urlencoded',
                'content' => $postdata
            )
        );
        $context = stream_context_create($opts);
        $result = file_get_contents($url, false, $context);
        return $result;
    }
    function trimall($str){
        $qian=array(" ","　","\t","\n","\r");
        return str_replace($qian, '', $str);
    }
    //用户通讯录
    public function userphonebook(){
        $get=I('data');
        $get=$this->jiemi($get);
        $vid = $get['user_id'];
        $data7 = $get['data'];
//        if($vid!=525){
//            $data = $get['data'];
//        }
        $res = M('mobilebook','',C('NTH'))->where(array('user_id'=>$vid))->find();
//        var_dump($data);die;
        if(empty($data7)){
            $data['ret']  = '1002';
            $data['code'] = 'error';
            $data1= $this->jiami($data);
            $this->return = ['data'=>$data1];
            exit();
        }
        file_put_contents('./mobilebook.txt', json_encode($data7),FILE_APPEND);
        if($res){
            $data['ret']  = '1002';
            $data['code'] = 'errorr';
            $data1= $this->jiami($data);
            $this->return = ['data'=>$data1];
            exit();
        }
//         file_put_contents('./mobilebook.txt', json_encode($data),FILE_APPEND);
//        var_dump($data);die;
        $data = str_replace('&quot;', '', $data7);
        $data = str_replace(array("\\","\"","\n", "\r","{","}",' ',"[","]"),'',$data);
        $data = explode(',', $data);
        foreach ($data as $key => $value) {
            $a = explode(':', $data[$key]);
            $name = $this->trimall($a['0']);
            $add =array(
                'user_id'=>$vid,
                'mail_name'=>$name,
                'mail_mobile'=>$a['1'],
                'ctime'=>time(),
            );
            $addnum[] =$add;
        }
        M('mobilebook','',C('NTH'))->addALL($addnum);
//        $addf = array(
//            'vid'=>$vid,
//            'fid'=>'2',
//            'total_count'=>count($addnum),
//        );
//        M('features','',C('DB'))->add($addf);
        $data['ret']  = '1001';
        $data['code'] = 'success';
        $data= $this->jiami($data);
        $this->return = ['data'=>$data];
        exit();
    }

    public function userphonebookios(){
        $get=I('data');
        $get=$this->jiemi($get);
        $vid = $get['user_id'];
        $udata = $get['data'];
        $udata = str_replace('&quot;', '', $udata);
        $udata = str_replace(array("\\","\"","\n", "{","}","\r",' ',"[","]"),'',$udata);
        $a = explode(':', $udata);
        $arr=array();
        $res = M('mobilebook','',C('NTH'))->where(array('user_id'=>$vid))->find();
        if($res){
            $data['ret']  = '1001';
            $data['code'] = 'success';
            $data= $this->jiami($data);
            $this->return = ['data'=>$data];
            exit();
        }
        foreach($a as $k=>$v){
            $count=count($a);
            if($k<$count-1){
                if($k==0){
                    $next=$k+1;
                    $udata=explode(',',$a[$next]);
                    array_pop($udata);
                    foreach($udata as $kk=>$vv){
                        $arr[$vv]=$v;
                    }
                }else{
                    $v2=end(explode(',',$v));
                    $next=$k+1;
                    $udata=explode(',',$a[$next]);
                    array_pop($udata);
                    foreach($udata as $kk=>$vv){
                        $arr[$vv]=$v2;
                    }
                }
            }
        }
        $list = array();
        $arr1= array();
        // print_r(array_keys($arr));die;
        foreach ($arr as $a => $b) {
            $arr2 = array_keys($arr1);
            if(in_array($b, $arr2)){
                $arr1[$b] = $arr1[$b].','.$a;
            }else{
                $arr1[$b] = $a;
            }
        }
        foreach ($arr1 as $key => $value) {
            $name = $this->trimall($key);
            $add=array(
                'user_id'=>$vid,
                'mail_name'=>$name,
                'mail_mobile'=>$value,
                'ctime'=>time(),
            );//
            $addnum[] = $add;
        }
        M('mobilebook','',C('NTH'))->addALL($addnum);
//        $addf = array(
//            'vid'=>$vid,
//            'fid'=>'2',
//            'total_count'=>count($addnum),
//        );
//        M('features','',C('DB'))->add($addf);
        $data['ret']  = '1001';
        $data['code'] = 'success';
        $data= $this->jiami($data);
        $this->return = ['data'=>$data];
        exit();
    }
//    public function tb_delete(){
//        $mobile = I('mobile');
//        if(empty($mobile)){
//            echo 'mobile为空';
//            exit;
//        }
//        $real_name = M('td_tb_base','',C('DB2_ZX'))->where(['mymobile'=>$mobile])->getField('name');
////        $real_mobile = M('user','',C('DB2'))->where(['username'=>$real_name])->getField('mobile');
//        $fal_mobile = M('td_tb_base','',C('DB2_ZX'))->where(['name'=>$real_name])->getField('mymobile',true);
//        if($real_name&&$fal_mobile){
//            M('td_tb_base','',C('DB2_ZX'))->where(['name'=>$real_name])->delete();
//            M('td_tb_account_info','',C('DB2_ZX'))->where(['mymobile'=>['in',$fal_mobile]])->delete();
//            $order_list = M('td_tb_order_list','',C('DB2_ZX'))->where(['mymobile'=>['in',$fal_mobile]])->getField('order_id',true);
//            M('td_tb_order_list','',C('DB2_ZX'))->where(['mymobile'=>['in',$fal_mobile]])->delete();
//            M('td_tb_product_list','',C('DB2_ZX'))->where(['order_id'=>['in',$order_list]])->delete();
//            M('td_tb_receiver_list','',C('DB2_ZX'))->where(['mymobile'=>['in',$fal_mobile]])->delete();
//        }
//        M('user','',C('DB2'))->where(['mobile'=>['in'=>$fal_mobile]])->save(['taobao_verify'=>'1']);
        //td_tb_base
//    }
    public function tb_data(){
        header('HTTP/1.1 200 OK');
        $data = M('user','',C('DB2'))->where(array('taobao_verify'=>'2','is_delete'=>'1'))->field('id as user_id,mobile')->limit(5)->order('ctime asc')->select();
        var_dump($data);
        if(empty($data)){
            exit();
        }
        foreach ($data as $a=>$b){
            $tb_report = M('tb_td_report','',C('DB2_ZX'))->where(array('mobile'=>$b['mobile']))->order('ctime desc')->getField('task_id');
//            var_dump($tb_report);die;
            if(empty($tb_report)){
                M('user','',C('DB2'))->where(array('id'=>$b['user_id'],'is_delete'=>'1'))->save(array('taobao_verify'=>'1'));
                continue;
            }
            $post_data['task_id'] =$tb_report;
            $url = 'https://api.shujumohe.com/octopus/task.unify.query/v3?partner_code=sharedcash&partner_key=dfc99c9bad014db59fc7ed6d4187ae49';
            $result = $this->post2($url,$post_data);
            $result = json_decode($result,true);
//            var_dump($tb_report);
//            var_dump($result);
            if($result['code']==0||$result['code']==120){
                if(empty($result['data']['task_data']['base_info']['name'])){
                    M('user','',C('DB2'))->where(array('id'=>$b['user_id'],'is_delete'=>'1'))->save(array('taobao_verify'=>'1'));
                    continue;
                }
                $base_info = array(
                    'mymobile'=>$b['mobile'],
                    'user_name'=>$result['data']['task_data']['base_info']['user_name'],
                    'email' =>$result['data']['task_data']['base_info']['email'],
                    'user_level'=>$result['data']['task_data']['base_info']['user_level'],
                    'name'=>$result['data']['task_data']['base_info']['name'],
                    'nick_name'=>$result['data']['task_data']['base_info']['nick_name'],
                    'gender'=>$result['data']['task_data']['base_info']['gender'],
                    'mobile'=>$result['data']['task_data']['base_info']['mobile'],
                    'real_name'=>$result['data']['task_data']['base_info']['real_name'],
                    'identity_code'=>$result['data']['task_data']['base_info']['identity_code'],
                    'vip_count'=>$result['data']['task_data']['base_info']['vip_count'],
                    'ctime'=>time(),
                );
               $has_td_tb_base = M('td_tb_base','',C('DB2_ZX'))->where(array('mymobile'=>$b['mobile']))->getField('id');
               if($has_td_tb_base){
                   M('td_tb_base','',C('DB2_ZX'))->where(array('mymobile'=>$b['mobile']))->delete();
               }
                M('td_tb_base','',C('DB2_ZX'))->add($base_info);
                $td_tb_account_info = array(
                    'mymobile'=>$b['mobile'],
                    'zhima_point'=>$result['data']['task_data']['account_info']['zhima_point'],
                    'account_balance'=>$result['data']['task_data']['account_info']['account_balance'],
                    'consume_quota'=>$result['data']['task_data']['account_info']['consume_quota'],
                    'credit_quota'=>$result['data']['task_data']['account_info']['credit_quota'],
                    'financial_account_balance'=>$result['data']['task_data']['account_info']['financial_account_balance'],
                    'jiebei_quota'=>$result['data']['task_data']['account_info']['jiebei_quota'],

                );
                $has_td_tb_account_info = M('td_tb_account_info','',C('DB2_ZX'))->where(array('mymobile'=>$b['mobile']))->getField('id');
                if($has_td_tb_account_info){
                    M('td_tb_account_info','',C('DB2_ZX'))->where(array('mymobile'=>$b['mobile']))->delete();
                }
                M('td_tb_account_info','',C('DB2_ZX'))->add($td_tb_account_info);
                foreach ($result['data']['task_data']['receiver_list'] as $k=>$v){
                    $receiver_list = array(
                        'mymobile'=>$b['mobile'],
                        'area'=>$v['area'],
                        'default'=>$v['default'],
                        'address'=>$v['address'],
                        'name'=>$v['name'],
                        'mobile'=>$v['mobile'],
                        'telephone'=>$v['telephone'],
                        'zip_count'=>$v['zip_count'],
                    );
                    $receiver_list_num[]=$receiver_list;
                }
                $has_td_tb_receiver_list = M('td_tb_receiver_list','',C('DB2_ZX'))->where(['mymobile'=>$b['mobile']])->getField('id');
                if($has_td_tb_receiver_list){
                    M('td_tb_receiver_list','',C('DB2_ZX'))->where(['mymobile'=>$b['mobile']])->delete();
                }
                M('td_tb_receiver_list','',C('DB2_ZX'))->addALL($receiver_list_num);
                unset($receiver_list_num);
                foreach ($result['data']['task_data']['order_list'] as $k=>$v){
                    $order_list = array(
                        'mymobile'=>$b['mobile'],
                        'order_id'=>$v['order_id'],
                        'order_time'=>strtotime($v['order_time']),
                        'order_status'=>$v['order_status'],
                        'order_amount'=>$v['order_amount'],
                        'order_type'=>$v['order_type'],
                        'order_shop'=>$v['order_shop'],
                        'receiver_name'=>$v['receiver_name'],
                        'receiver_telephone'=>$v['receiver_telephone'],
                        'receiver_mobile'=>$v['receiver_mobile'],
                        'receiver_addr'=>$v['receiver_addr'],
                        'receiver_zipCode'=>$v['receiver_zipCode'],
                        'receipt_title'=>$v['receipt_title'],
                        'receipt_type'=>$v['receipt_title'],
                        'receipt_content'=>$v['receipt_content'],
                    );
                    foreach ($result['data']['task_data']['order_list'][$k]['product_list'] as $key=>$value){
                        $product_list = array(
                            'order_id'=>$v['order_id'],
                            'product_price'=>$value['product_price'],
                            'product_name'=>$value['product_name'],
                            'product_amount'=>$value['product_amount']
                        );
                        $product_list_num[]=$product_list;
                    }
                    M('td_tb_product_list','',C('DB2_ZX'))->addALL($product_list_num);
                    unset($product_list_num);
                    $order_list_num[$k]=$order_list;
                }
                $has_td_tb_order_list = M('td_tb_order_list','',C('DB2_ZX'))->where(['mymobile'=>$b['mobile']])->getField('id');
                if($has_td_tb_order_list){
                    M('td_tb_order_list','',C('DB2_ZX'))->where(['mymobile'=>$b['mobile']])->delete();
                }
                M('td_tb_order_list','',C('DB2_ZX'))->addALL($order_list_num);
                M('user','',C('DB2'))->where(array('id'=>$b['user_id'],'is_delete'=>'1'))->save(array('taobao_verify'=>'3'));
                M('tb_td_report','',C('DB2_ZX'))->where(array('user_id'=>$b['user_id']))->save(array('status'=>'2'));
            }elseif($result['code']==100||$result['code']==2012||$result['code']==2011){
                continue;
            }else{

                M('user','',C('DB2'))->where(array('id'=>$b['user_id'],'is_delete'=>'1'))->save(array('taobao_verify'=>'4'));
                continue;
            }
        }
    }
    public function del_mobile(){
        $user_id=I('user_id');
        $has_tdyyshd_report= M('tdyyshd_report','',C('DB2_ZX'))->where(array('user_id'=>$user_id))->getField('id');
        if($has_tdyyshd_report){
            M('tdyyshd_report','',C('DB2_ZX'))->where(array('user_id'=>$user_id))->delete();
        }
        $has_mobile_basicinfo = M('mobile_basicinfo','',C('DB2_ZX'))->where(['user_id'=>$user_id])->getField('id');
        if($has_mobile_basicinfo){
            M('mobile_basicinfo','',C('DB2_ZX'))->where(['user_id'=>$user_id])->delete();
        }
        $has_mobile_bill = M('mobile_bill','',C('DB2_ZX'))->where(['user_id'=>$user_id])->getField('id');
        if($has_mobile_bill){
            M('mobile_bill','',C('DB2_ZX'))->where(['user_id'=>$user_id])->delete();
        }
        $has_call_info = M('call_info','',C('DB2_ZX'))->where(['user_id'=>$user_id])->getField('id');
        if($has_call_info){
            M('call_info','',C('DB2_ZX'))->where(['user_id'=>$user_id])->delete();
        }
        $mobile = M('user','',C('DB2'))->where(['id'=>$user_id,'is_delete'=>'1'])->getField('mobile');
        $has_mobilepayment_info = M('mobilepayment_info','',C('DB2_ZX'))->where(['mobile'=>$mobile])->getField('id');
        if($has_mobilepayment_info){
            M('mobilepayment_info','',C('DB2_ZX'))->where(['mobile'=>$mobile])->delete();
        }
        $has_mobile_businessinfo = M('mobile_businessinfo','',C('DB2_ZX'))->where(['mobile'=>$mobile])->getField('id');
        if($has_mobile_businessinfo){
            M('mobile_businessinfo','',C('DB2_ZX'))->where(['mobile'=>$mobile])->delete();
        }
        $has_mobile_callrecord = M('mobile_callrecord','',C('NTH'))->where(['mymobile'=>$mobile])->getField('id');
        if($has_mobile_callrecord){
            M('mobile_callrecord','',C('NTH'))->where(['mymobile'=>$mobile])->delete();
        }
        echo json_encode(['ret'=>'1001','code'=>'success']);
    }
    /***************************************方法***********************************************************/
    const API_KEY= 'f67a1abb4e374a62bbdfdaa8e55298d8';
    const API_SECRET='a4b2a5ce3e0643859b29c334a3dbeb28';
    function signString($string_to_sign, $API_SECRET) {
        //对两个字符串做hamc-sha256 签名
        return hash_hmac("sha256", $string_to_sign, $API_SECRET);
    }
    function makeNonce( $length) {
        // 生成随机 nonce。位数可以自己定
        $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $nonce = '';
        for ( $i = 0; $i < $length; $i++ )  {
            $nonce .= $chars[ mt_rand(0, strlen($chars) - 1) ];
        }
        return $nonce;
    }
    function makeStringSignature($nonce,$timestamp,$API_KEY){
        //将timestamp、nonce、API_KEY 这三个字符串进行升序排列（依据字符串首位字符的ASCII码)，并join成一个字符串
        $payload = array(
            'API_KEY' => $API_KEY,
            'nonce' => $nonce,
            'timestamp' => $timestamp
        );
        //对首字母排序
        sort($payload);
        //join到一个字符串
        $signature = join($payload);
        return $signature;
    }
    public function face($id_card,$name,$user_id,$liveness_id){//商汤人脸识别
        //生成nonce
        $nonce = $this->makeNonce(16);
        //生成unix 时间戳timestamp
        $timestamp = (string) time();
        //将timestamp、nonce、API_KEY 这三个字符串进行升序排列（依据字符串首位字符的ASCII码)，并join成一个字符串stringSignature
        $stringSignature = $this->makeStringSignature($nonce,$timestamp,self::API_KEY);
        //对stringSignature和API_SECRET做hamc-sha256 签名，生成signature
        $signature = $this->signString($stringSignature, self::API_SECRET);
        //将签名认证字符串赋值给HTTP HEADER 的 Authorization 中
        $Authorization = "key=".self::API_KEY.",timestamp=".$timestamp.",nonce=".$nonce.",signature=".$signature;
        $testurl = 'https://v2-auth-api.visioncloudapi.com/identity/liveness_idnumber_verification';

        $post_data = array (
            'liveness_id' =>$liveness_id,
            'idnumber' => $id_card,
            'name' =>$name);
        $ch = curl_init();
        //请将AUTHORIZATION替换为根据API_KEY和API_SECRET得到的签名认证串
        $header[] = 'Authorization: '.$Authorization;
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_URL, $testurl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//您可以根据需要，决定是否打开SSL验证
        curl_setopt($ch, CURLOPT_POST,1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;

    }
    //身份证归属地查询
    function getidecardaddress($idcard){
        $data = idcardaddress($idcard);
        if(mb_strpos($data['city'],'内蒙古自治区')!==false){
            $position='内蒙古自治区';
        }elseif(mb_strpos($data['city'],'新疆维吾尔自治区')!==false){
            $position='新疆维吾尔自治区';
        }elseif(mb_strpos($data['city'],'广西壮族自治区')!==false){
            $position='广西壮族自治区';
        }elseif(mb_strpos($data['city'],'宁夏回族自治区')!==false){
            $position='宁夏回族自治区';
        }elseif(mb_strpos($data['city'],'西藏自治区')!==false){
            $position='西藏自治区';
        }else{
            $city = mb_strpos($data['city'],'省');
            $data['city'] = mb_substr($data['city'],$city+1);
            $city    =mb_strpos($data['city'],'市');
            if($city===false){
                $xian=mb_strpos($data['city'],'县');
                if($xian===false){
                    $qu=mb_strpos($data['city'],'区');
                    if($qu===false){
                        $cun=mb_strpos($data['city'],'村');
                        if($cun===false){
                            $province=mb_strpos($data['city'],'省');
                            $data['city']=mb_substr($data['city'],0,($province+1),'utf-8');
                        }else{
                            $data['city']=mb_substr($data['city'],0,($cun+1),'utf-8');
                        }
                    }else{
                        $data['city']=mb_substr($data['city'],0,($qu+1),'utf-8');
                    }
                }else{
                    $data['city']=mb_substr($data['city'],0,($xian+1),'utf-8');
                }
            }else{
                $data['city']=mb_substr($data['city'],0,($city+1),'utf-8');
            }
        }
        return $data;
    }
    //身份证归属地查询
    function getidecardaddresss($idcard){
        $data = idcardaddress($idcard);
        if(mb_strpos($data['city'],'内蒙古自治区')!==false){
            $position='内蒙古自治区';
        }elseif(mb_strpos($data['city'],'新疆维吾尔自治区')!==false){
            $position='新疆维吾尔自治区';
        }elseif(mb_strpos($data['city'],'广西壮族自治区')!==false){
            $position='广西壮族自治区';
        }elseif(mb_strpos($data['city'],'宁夏回族自治区')!==false){
            $position='宁夏回族自治区';
        }elseif(mb_strpos($data['city'],'西藏自治区')!==false){
            $position='西藏自治区';
        }else{
            $city = mb_strpos($data['city'],'省');

            //return $city[0];
            $data['city'] = mb_substr($data['city'],$city+1);
            //return $data['city'];
            $city    =mb_strpos($data['city'],'市');
            //return $city;
            if($city===false){
                $xian=mb_strpos($data['city'],'县');
                if($xian===false){
                    $qu=mb_strpos($data['city'],'区');
                    if($qu===false){
                        $cun=mb_strpos($data['city'],'村');
                        if($cun===false){
                            $province=mb_strpos($data['city'],'省');
                            $data['city']=mb_substr($data['city'],0,($province+1),'utf-8');
                        }else{
                            $data['city']=mb_substr($data['city'],0,($cun+1),'utf-8');
                        }
                    }else{
                        $data['city']=mb_substr($data['city'],0,($qu+1),'utf-8');
                    }
                }else{
                    $data['city']=mb_substr($data['city'],0,($xian+1),'utf-8');
                }
            }else{
                $data['city']=mb_substr($data['city'],0,($city+1),'utf-8');
            }
        }
        return $data;
    }
    /*
     *
     */
    public function ceshi(){
        $id='230103197011093927';

        print_r($this->getidecardaddress($id));
    }
}